<?php
/**********************
	Quicklog v1.1
  phphackr@gmail.com
***********************/
include_once 'config.php';
include_once 'time.php';

 ////////////////////////////
// Post Section
if($_POST['submit'] && $_POST['add'] != ''){
	$input = stripslashes(strip_tags($_POST['add']));
	preg_match('p:[a-zA-Z0-9\s]:', '', $input, $plazes);
	$input = preg_replace('/p:[a-zA-Z0-9\s]:/', '', $input);
	$time = time();
	$new_line = "\n".$time.'|'.$input;
	$plazes = ($_POST["plazes"] == true) ? true : false;
	if($_POST['twitter'] === true):
		$cred = sprintf('Authorization: Basic %s', base64_encode("$twitter_username:$twitter_password"));
		$content = http_build_query(array('status'=>$input));
		$opts = array(
			'http'=>array(
			'method'=>'POST',
			'header'=>$cred,
			'content'=>$content
		)
		);
		$ctx = stream_context_create($opts);
		$success=@file_get_contents('http://twitter.com/statuses/update.xml?'.$content.'&source=quicklog',false,$ctx);
		echo 'http://twitter.com/statuses/update.xml?'.$content."\n";
	endif;
	/*
	if($_POST['plazes']):
		$cred = sprintf('Authorization: Basic %s', base64_encode("$plazes_username:$plazes_password"));
		preg_match('p:[a-zA-Z0-9\s]:', '', $input, $match);
		$stripped = preg_replace('/p:[a-zA-Z0-9\s]:/', '', $input, $match);
		$content = '<activity>'."\n\t".'<status>'.$input.'</status>'."\n\t".'<plaze>'."\n\t\t".'<name>'.$plazes.'</name>'."\n\t".'</plaze>'."\n".'</activity>';
		$opts = array(
			'http'=>array(
			'method'=>'POST',
			'header'=>$cred,
			'content'=>$content
		)
		);
		$ctx = stream_context_create($opts);
		$success=@file_get_contents('http://plazes.com/activities.xml',false,$ctx);
	endif;
	*/
    if(@file_put_contents($file, file_get_contents($file).$new_line)){
    	echo "Success";
	}
	exit;
}

 ///////////////////////
//Ajax Section
if($_GET['entries']){
	$i=0;
	header('Content-type: text/html');
	foreach(array_reverse(file($file, FILE_SKIP_EMPTY_LINES)) as $key => $line){
	$i++;
		if($line != '' && $i < 10){
			$line = explode('|', $line);
			$time = timeDiff($line[0], array('precision' => 'minute'));
			$line = $line[1];
			echo ($i == 0) ? '<div class="entry">' : '<div class="first entry">';
			echo '<span class="time">'.$time.':</span><br /> '.$line."</div>";
		}
	}
	exit;
}

/*************************************
Displays content-type based on browser
***************************************/
$browser = get_browser(null);
//if(stristr('Mozilla', $browser->parent)||stristr('Firefox', $browser->parent)||stristr('Opera', $browser->parent))
header('Content-type: application/xhtml+xml');
//else
//header('Content-type: application/xml');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Quicklog</title>
    <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
    <link rel="alternate" type="application/rss+xml" title="RSS" href="rss.xml" />
	<link rel="stylesheet" type="text/css" href="css/style.css" />
	<link rel="stylesheet" type="text/css" href="css/reset.css" />
	<script src="js/jquery.js" type="text/javascript"></script>
	<script type="text/javascript">
	  $(document).ready(function(){
	   $("#add").focus();
	   $("#quicklog").submit(function(){ 
	   	$.post("index.php",{ add: $("#add").val(), twitter: $("#twitter").attr('checked'), submit: 'submit'}, function() {
	   		$("#add").val('');
	   		$("#log").slideDown("fast");
	   		setTimeout('$("#log").slideUp("fast")', 3000);
	   		updateEntries();
	   	});
	   	return false;
	   });
	 });
	 $(document).ready(function(){
	 	updateEntries();
	 }); 
	 function updateEntries(){
	 	$("#entries").load("index.php?entries=true");
	 	setTimeout('updateEntries()', 5500);
	 }
	</script>
</head>
<body>
	<div class="container">
	    <h1><img src="img/logo.png" alt="Quicklog" title="Quicklog" /></h1>
		<div class="menu">Home | <a href="./about.php">About</a> | <a href="./archive.php">Archives &rarr;</a></div>
<?if($_SERVER['REMOTE_ADDR'] == '65.24.48.140'||$_SERVER['REMOTE_ADDR'] == '127.0.0.1'):?>
	    <div id="log">Entry Added</div>
	    <form id="quicklog" action="index.php" method="post" accept-charset="utf-8">
		<fieldset>
			<input type="text" name="add" id="add" />
			<input type="submit" name="submit" id="submit" value="Add Action" /><br />
<? if($twitter_username !== '' && $twitter_password !== ''): ?>
			<input type="checkbox" name="twitter" id="twitter" checked="checked" /> Post to Twitter
<? endif; ?>
<? if($plazes_username !== '' && $plazes_password !== ''): ?>
			<input type="checkbox" name="plazes" id="plazes" checked="checked" /> Post to Plazes
<? endif; ?>
		</fieldset>
	    </form>
	    <?endif;?>
		<div id="entries">
<?
			foreach(array_reverse(file($file, FILE_SKIP_EMPTY_LINES)) as $key => $line){
			$i++;
				if($line != '' && $i<10){
				$line = explode('|', $line);
				$time = timeDiff($line[0], array('precision' => 'minute'));
				$line = $line[1];
				echo '		<div class="entry">
			<span class="time">'.$time.':</span><br /> 
			'.rtrim($line).'
		</div>
';
				}
			}?>
		</div>
	</div>
	<p id="valid">
		<a href="http://validator.w3.org/check?uri=referer">
			<img src="http://www.w3.org/Icons/valid-xhtml11-blue" alt="Valid XHTML 1.1" height="31" width="88" />
		</a>
	</p>
</body>
</html>